package com.example.modelrepositorydemo.Service;

public class ArtifactVersionServiceImpl implements ArtifactVersionService {
    private final ArtifactRepository artifactRepository;
    private final ArtifactVersionRepository artifactVersionRepository;

    public ArtifactVersionServiceImpl(ArtifactRepository artifactRepository, ArtifactVersionRepository artifactVersionRepository) {
        this.artifactRepository = artifactRepository;
        this.artifactVersionRepository = artifactVersionRepository;
    }

    @Override
    public void createArtifactVersion(String groupName, String artifactName, String version) {
        Artifact artifact = artifactRepository.findByNameAndGroup(groupName, artifactName);
        if (artifact == null) {
            throw new ResourceNotFoundException(String.format("Artifact '%s' not found in group '%s'", artifactName, groupName));
        }
        ArtifactVersion artifactVersion = new ArtifactVersion();
        artifactVersion.setArtifact(artifact);
        artifactVersion.setVersion(version);
        artifactVersionRepository.save(artifactVersion);
    }
}